package net.citrite.android;

import com.android.sdklib.IAndroidTarget;
import com.android.sdklib.devices.Device;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.LogManager;

public class Main {

    public static void main(String[] args) {
        Map<String, String> parameters = new HashMap<>();
        String key = null;
        for (String arg : args) {
            if (arg.startsWith("-")) {
                if (arg.equals("-p")) {
                    key = "project";
                } else if (arg.equals("-s")) {
                    key = "summary";
                } else if (arg.equals("-d")) {
                    key = "detail";
                } else if (arg.equals("-v")) {
                    System.out.printf("1.0");
                    System.exit(0);
                } else {
                    usage();
                }
                if (!parameters.containsKey(key)) {
                    parameters.put(key, "");
                } else {
                    usage();
                }
            } else if (key != null && parameters.containsKey(key)){
                parameters.put(key, arg);
            }

        }
        //File file = new File("/Users/taodongl/AndroidStudioProjects/test/app/src/main/AndroidManifest.xml");
        if (!parameters.containsKey("project")) {
            usage();
        }
        initLogger(parameters.containsKey("detail") ? parameters.get("detail") : null,
                parameters.containsKey("summary") ? parameters.get("summary") : null);
        File manifest = new File(parameters.get("project"));
        try {
            net.citrite.android.Project project = net.citrite.android.Project.createFromManifest(manifest);
            PreviewManager manager = new PreviewManager();
            //manager.setAndroidSdk("/Users/taodongl/Library/Android/sdk");
            IAndroidTarget target = manager.getDefaultTarget();
            Device device = manager.getDefaultDevice();
            RenderService renderService = new RenderService(target, device);
            renderService.render(project);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //XmlNodeReport.parse(new File("C:\\sourcecode\\CitrixGateway\\res\\layout\\activity_transparent_enable_log.xml"));
        System.exit(0);
    }

    private static void initLogger(String report, String summary) {
        LogManager logManager = LogManager.getLogManager();
        final String sep = System.getProperty("line.separator");
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("report.csv.handlers = net.citrite.android.logger.DetailReportHandler");
        stringBuilder.append(sep);
        stringBuilder.append("net.citrite.android.logger.DetailReportHandler.level=INFO");
        stringBuilder.append(sep);
        stringBuilder.append("net.citrite.android.logger.DetailReportHandler.append=false");
        stringBuilder.append(sep);
        stringBuilder.append(String.format("net.citrite.android.logger.DetailReportHandler.pattern=%s", (report == null) ? "detail_result.csv" : report));
        stringBuilder.append(sep);
        stringBuilder.append("net.citrite.android.logger.DetailReportHandler.encoding=UTF-8");
        stringBuilder.append(sep);
        stringBuilder.append("net.citrite.android.logger.DetailReportHandler.formatter=net.citrite.android.logger.DetailReportFormatter");
        stringBuilder.append(sep);


        stringBuilder.append("summary.csv.handlers = net.citrite.android.logger.SummaryReportHandler");
        stringBuilder.append(sep);
        stringBuilder.append("net.citrite.android.logger.SummaryReportHandler.level=INFO");
        stringBuilder.append(sep);
        stringBuilder.append("net.citrite.android.logger.SummaryReportHandler.append=false");
        stringBuilder.append(sep);
        stringBuilder.append(String.format("net.citrite.android.logger.SummaryReportHandler.pattern=%s", (summary == null) ? "summary_result.csv" : summary));
        stringBuilder.append(sep);
        stringBuilder.append("net.citrite.android.logger.SummaryReportHandler.encoding=UTF-8");
        stringBuilder.append(sep);
        stringBuilder.append("net.citrite.android.logger.SummaryReportHandler.formatter=net.citrite.android.logger.SummaryReportFormatter");
        stringBuilder.append(sep);

        stringBuilder.append("net.citrite.android.RenderService.handlers=java.util.logging.ConsoleHandler");
        stringBuilder.append(sep);
        stringBuilder.append("net.citrite.android.Project.handlers=java.util.logging.ConsoleHandler");
        stringBuilder.append(sep);
        stringBuilder.append("net.citrite.android.PreviewManager.handlers=java.util.logging.ConsoleHandler");
        stringBuilder.append(sep);
        stringBuilder.append("java.util.logging.ConsoleHandler.formatter=net.citrite.android.logger.TruncationFormatter");
        stringBuilder.append(sep);
        stringBuilder.append("java.util.logging.ConsoleHandler.level=CONFIG");
        stringBuilder.append(sep);
        InputStream stream = new ByteArrayInputStream(stringBuilder.toString().getBytes());
        try {
            logManager.readConfiguration(stream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void usage() {
        System.out.printf("java -Dsdk==<sdk> -cp <sdkLib>\\*;TruncationDetector4Android.jar net.citrite.android.Main -p <manifest> " +
                "[-s summary_result.csv] [-d detail_result.csv] [-v]");
        System.exit(0);
    }
}
